<script>
import Vue from 'vue'
import { Form, Field, Button, NavBar, Toast } from 'vant'
import { loginAPI } from '@/api/user'

Vue.use(Form)
Vue.use(Field)
Vue.use(Button)
Vue.use(NavBar)
Vue.use(Toast)

export default {
  name: 'LoginView',
  components: {},
  setup () {
    return {}
  },
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await loginAPI({
        username: this.username,
        password: this.password
      })
      Toast.success('登录成功')
      await this.$router.push('/home')
      console.log('res', res)
    }
  }
}
</script>

<template>
  <div>
    <div class="tittle">
      登录
    </div>
    <van-form @submit="onSubmit" class="loginForm">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="css" scoped>
.tittle {
  text-align: center;
  margin-top: 100px;
  font-size: 25px;
  color: #333333;
}

.loginForm {
  margin-top: 50px;
  padding: 20px;
}

</style>
